package com.UdpRecieve.controller;

import com.UdpRecieve.controller.system.BaseController;
import com.UdpRecieve.domain.FileOption;
import com.UdpRecieve.framework.annotation.LogAnnotion;
import com.UdpRecieve.framework.enums.BusinessType;
import com.UdpRecieve.framework.page.TableDataInfo;
import com.UdpRecieve.service.SysSystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/SystemConfigPage")
public class SystemConfigController extends BaseController {

    @Autowired
    private SysSystemConfigService sysSystemConfigService;

    @GetMapping()
    public ModelAndView toSystemConfigPage(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("system/option_system");
        return mv;
    }


    /**
     * 查询系统配置列表
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo selectSystemOption(){
        startPage();
        List<FileOption> sysConfigList =  sysSystemConfigService.selectConfigList();
        return getDataTable(sysConfigList);
    }

    @GetMapping("/edit/{id}")
    public ModelAndView toEditPage(@PathVariable("id")Long id){
        ModelAndView mv = new ModelAndView();
        List<FileOption> fileOptions =  sysSystemConfigService.selectConfigList();
        mv.addObject("fileOptions",fileOptions.get(0));
        mv.setViewName("system/option_system_edit");
        return mv;
    }

    /**
     * 修改保存
     */
    @PostMapping("/edit")
    @ResponseBody
    @LogAnnotion(title = "系统管理_系统配置",businessType = BusinessType.UPDATE)
    public ModelMap editSave(FileOption fileOption) {
        ModelMap m = new ModelMap();
        int result = -1;
        try {
            result = sysSystemConfigService.updateConfig(fileOption);
        }catch (Exception e){
            String msg = e.getMessage();
            m.addAttribute("msg",msg);
        }finally {
            m.addAttribute("result",result);
            return m;
        }
    }
}
